package main

import (
	"bufio"
	"github.com/tealeg/xlsx"
	"os"
	"strconv"
	"strings"
)

const ExcelFileName = "data.xlsx"

var baseUrl = "https://dztbdweb-prod.groupama-avic.com.cn:9007/" //正式环境
// var baseUrl = "https://dztbdweb-test.groupama-avic.com.cn:9007/" //测试环境
var makeSignUrl = "http://uat-newcar.groupama-avic.com.cn:8001/" //制作签名图片的Url
func main() {
	CreateFolder()
	sheet := LoadXlsSheet()
	currentLine := 0
	println("第1行是表头，默认不处理，开始加载第一个sheet表")
	for {
		println("\n========================")
		println("\nEnter：开始操作\n0:退出\n大于0:跳转到目标行并操作")
		println("请输入功能编码：")
		inputText, _ := bufio.NewReader(os.Stdin).ReadString('\n')
		inputText = strings.TrimSpace(inputText)
		inputNumber, _ := strconv.Atoi(inputText)
		if inputText == "" {
			currentLine++
			Operate(currentLine, sheet)
		} else if inputNumber == 0 {
			println("已退出")
			break
		} else if inputNumber > 0 {
			Operate(inputNumber, sheet)
		} else {
			println("无操作")
		}
	}
}
func Operate(currentLine int, sheet *xlsx.Sheet) {
	println(sheet.Rows)
	println("开始操作第" + strconv.Itoa(currentLine+1) + "行,总共" + strconv.Itoa(len(sheet.Rows)) + "行")
	row := sheet.Rows[currentLine]
	policyNumber := row.Cells[0].String()
	name := row.Cells[1].String()
	proposalNo := GetProposalNo(row.Cells[2].String())
	println("第一步：本行信息：" + policyNumber + "|" + name + "|" + proposalNo)
	url := baseUrl + "TokenProduce/save/" + proposalNo
	apiResponse, err := RequestJson("POST", url, map[string]interface{}{})
	if err != nil {
		println("请求或解析错误：" + err.Error())
		return
	} else if strconv.Itoa(apiResponse.Code) == "401" {
		println(apiResponse.Message)
		return
	} else if strconv.Itoa(apiResponse.Code) == "403" {
		println("第二步：这一行已经签字完成")
		println("_____________________________________")
	} else {
		println("第二步：这一行还没有签字,开始制作" + name + "的签名图片")
		path, e := DownloadBase64(makeSignUrl+"prpall/signInsuredName/getImg?insuredName="+name, "sign"+"/preview_"+name+".png")
		if e != nil {
			println("下载base64图片错误：" + e.Error())
			return
		}
		if path == "" {
			println("下载签名文件失败，图片路径为空")
			return
		}
		println("第三步：开始上传签名文件")
		println(string(apiResponse.Data))
		var jsonData, err = ParseBean[map[string]interface{}](apiResponse.Data)
		if err != nil {
			println("解析json失败：" + err.Error())
			return
		}
		err = UploadFile(path, baseUrl+"signature/"+proposalNo, "Bearer "+jsonData["token"].(string), proposalNo)
		if err != nil {
			println("上传文件失败：" + err.Error())
			return
		}
		println("_____________________________________")
	}
}
